package com.coffee.core;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;
import java.util.List;

/**
 * @author songkui
 * @since 2024/3/26 17:05
 */
public class EnumValidator  implements ConstraintValidator<EnumValid, String> {
    private List<String> enumStringList;

    @Override
    public void initialize(EnumValid constraintAnnotation) {
        enumStringList = Arrays.asList(constraintAnnotation.value());
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if(value == null){
            return true;
        }
        //可以 自定义字典校验
        return enumStringList.contains(value);
    }
}
